package other_其他模式.DAO_data_access_object_数据访问对象模式;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: hihi
 * @Date: 2020/07/10/11:03
 * @Description:
 */
public class StudentDaoImpl implements StudentDao {
	//模拟数据库
	private static Set<Student> studentList = new HashSet<>();

	static {
		studentList.add(new Student(1, "zhangsan1"));
		studentList.add(new Student(2, "zhangsan2"));
		studentList.add(new Student(3, "zhangsan3"));
		studentList.add(new Student(4, "zhangsan4"));
	}

	@Override public Student getById(int id) {

		List<Student> collect = studentList.stream().filter(student -> student.getId() == id).collect(Collectors.toList());
		return collect.get(0);
	}

	@Override public void save(Student student) {
		studentList.add(student);
	}

	@Override public void updateById(Student student) {
		Student student1 = getById(student.getId());
		studentList.remove(student1);
		save(student);
	}

	@Override public Set<Student> getAll() {
		return studentList;
	}
}
